package com.tangula.web.kashine.business;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.tangula.web.form.DefaultPagingBody;
import com.tangula.web.kashine.entity.AwardForm;
import com.tangula.web.kashine.entity.SendAwardForm;
import com.tangula.web.kashine.entity.StudentAwardRecordEntity;
import com.tangula.web.kashine.entity.StudentPageableForm;
import com.tangula.web.kashine.repository.AccountEntityRepository;
import com.tangula.web.kashine.repository.ProductEntityRepository;
import com.tangula.web.kashine.repository.StoreRepository;
import com.tangula.web.kashine.repository.StudentAwardRecordEntityRepository;
@Service
public class StudentAwardBusinessImpl implements StudentAwardBusiness{
	@Autowired
	AccountEntityRepository accountRepository;
	@Autowired
	ProductEntityRepository productRepository;
	@Autowired
	StoreRepository storeRepository;
	@Autowired
	StudentAwardRecordEntityRepository	studentAwardRepository;
	@Override
	public StudentAwardRecordEntity findProductByTeacher(StudentAwardRecordEntity studentAward) {
		return studentAwardRepository.saveAndFlush(studentAward);
	}
	@Override
	public Boolean saveAwardToStudent(SendAwardForm send) {
		if(send!=null) {
			List<String> lists = send.getAwards();
			List<StudentAwardRecordEntity> ll = new ArrayList<StudentAwardRecordEntity>();
			for (String list : lists) {
				StudentAwardRecordEntity sare1 = studentAwardRepository.findById(list).get();
				StudentAwardRecordEntity sare2 = new StudentAwardRecordEntity();
				sare2.setAwardDesc(sare1.getAwardDesc());
				sare2.setAwardImage(sare1.getAwardImage());
				sare2.setAwardName(sare1.getAwardName());
				sare2.setStudentId(send.getStudentId());
				sare2.setSendTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				ll.add(studentAwardRepository.saveAndFlush(sare2));
			}
			if(ll.size()==lists.size()) {
				return true;
			}else {
				return false;
			}
		}else {
			return false;
		}
	}
	@Override
	public DefaultPagingBody<AwardForm> showAwaed(StudentPageableForm student) {
		if(student!=null) {
			PageRequest pr = PageRequest.of(student.getIndex()-1, student.getSize(), Sort.by(new Sort.Order(Direction.ASC, "id")));
				List<StudentAwardRecordEntity> sas =  studentAwardRepository.findByStudentId(student.getCondition(),pr);
				List<AwardForm> afs = new ArrayList<AwardForm>();
					for(int i=0;i<sas.size();i++) {
						AwardForm af = new AwardForm();
						af.setAwardDesc(sas.get(i).getAwardDesc());
						af.setAwardIcon(sas.get(i).getAwardImage());
						af.setAwardName(sas.get(i).getAwardName());
						afs.add(af);
					}
				
				 DefaultPagingBody<AwardForm> d = new DefaultPagingBody<>(student.getIndex(), student.getSize(), afs.size(), afs);	
			return d;
		}else {
			return null;
		}
	}

}
